package com.virtualtrainer_01.viewListeners;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.virtualtrainer_01.R;
import com.virtualtrainer_01.dao.ExerciseDB;
import com.virtualtrainer_01.model.Group;
import com.virtualtrainer_01.util.NameList;
import com.virtualtrainer_01.view.SetupTraining;

public class SetupListAdapter extends BaseExpandableListAdapter {

	static class ViewHolder {
		private CheckableLinearLayout layout;

		public void setLayout(CheckableLinearLayout l) {
			this.layout = l;
		}

		public CheckableLinearLayout getLayout() {
			return this.layout;
		}
	}

	private Context context;
	private String[] parentList;
	private String[][] childList;
	private boolean[][] checkedState;

	public SetupListAdapter(Context context, List<NameList<Group>> groups,
			int day) {
		if (groups.get(day).size() == 0)
			return;
		this.context = context;
		parentList = new String[groups.get(day).size()];
		for (int i = 0; i < parentList.length; i++)
			parentList[i] = groups.get(day).get(i).getName();
		ExerciseDB exerciseDb = new ExerciseDB(context);
		exerciseDb.open();
		childList = new String[parentList.length][];
		checkedState = new boolean[parentList.length][];

		for (int i = 0; i < parentList.length; i++) {
			childList[i] = exerciseDb.getExerciseNames(parentList[i]);
			checkedState[i] = new boolean[parentList[i].length()];
			Arrays.fill(checkedState[i], false);
		}
		exerciseDb.close();
	}

	@Override
	public int getGroupCount() {
		if (parentList != null)
			return parentList.length;
		else
			return 0;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (childList != null)
			return childList[groupPosition].length;
		else
			return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupPosition;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView tv = new TextView(context);
		tv.setText(parentList[groupPosition]);
		tv.setPadding(40, 0, 0, 0);
		tv.setTextSize(40);
		return tv;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {

			convertView = ((SetupTraining) context).getLayoutInflater()
					.inflate(R.layout.list_item, parent, false);
		}
		((TextView) convertView.findViewById(android.R.id.text1))
				.setText(childList[groupPosition][childPosition]);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public String[][] getChildList() {
		return childList;
	}

	public void setChildList(String[][] childList) {
		this.childList = childList;
	}
}
